<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Http\Requests\VideoRequest;
use Auth;
use App\Models\Video;
use App\Models\User;
class VideosController extends Controller
{
    public function __construct()
    {
        $this->middleware('auth');
    }

    public function index(Video $video, Request $request)
    {
        $user = Auth::user();
        if ($user->hasRole('Founder')) {
            $videos = $video->where('user_id', $user->id)->orderBy('updated_at', 'desc')->paginate(20);
            return view('videos.index', compact('videos', 'user'));
        }
    }
    public function create(Video $video)
    {
        $this->authorize('update', $video);
        $user = Auth::user();
        return view('videos.edit', compact('user', 'video'));
    }

    public function edit(Video $video, Request $request)
    {
        $this->authorize('update', $video);
        $user = Auth::user();
        return view('videos.edit', compact('user', 'video'));
    }
    public function store(Video $video, VideoRequest $request)
    {
        $link = $request->link;
        $video->link = $link;
        $video->user_id = Auth::id();
        // $video->create_type = 0;
        // $video->site_id = 0;
        $video->save();
        return redirect()->route('videos.index')->with('success', '添加成功！');
    }

    public function update(Video $video, VideoRequest $request)
    {
        $link = $request->link;
        $video->link = $link;
        $video->save();
        return redirect()->route('videos.index')->with('success', '更新成功！');
    }

    public function destroy(Video $video)
    {
        if(Auth::user()->hasRole('Founder')) $video->delete();
        return redirect()->route('videos.index')->with('success', '删除成功！');
    }
}
